﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InternshipManagement.Models
{

    public class CourseRepository
    {
        public InternshipEntities entities = new InternshipEntities();

        public IQueryable<Course> FindAllActiveCourses()
        {
            return entities.Courses.Where(c => c.Status == "A").OrderBy(c => c.CourseID);
        }

        public Course GetCourse(int id)
        {
            return entities.Courses.FirstOrDefault(c => c.CourseID == id);
        }

        public bool CourseExists(string code, int CourseID)
        {
            return entities.Courses.FirstOrDefault(c => c.CourseID == CourseID && c.Code == code && c.Status != "P") != null;
        }

        public void AddCourse(Course course)
        {
            entities.Courses.AddObject(course);
        }

        public void DeleteCourse(Course course)
        {
            entities.Courses.DeleteObject(course);
        }

        //
        // Persistence
        public void Save()
        {
            entities.SaveChanges();
        }
    }
}